// pages/addvote/addvote.js
const app = getApp()
var request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qId: 1,
    isAddTitle: false,
    title: "",
    isAdd: false, //当前是否在添加
    subjectTitle: '',
    isSubmit:false,
    currentType: 0, //当前添加的类型
    options: [
      // {
      //   id:1,
      //   option:"你在吗?",
      //   type:1
      // }
    ], //题目
    voteInfo: {
      qId: 0,
      title: "",
      subject: [
        // {
        //   subjectTitle:"",
        //   options:[]
        // }
      ]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let info = this.data.voteInfo;
    info.qId = options.qid;
    this.setData({
      qId: options.qid,
      voteInfo: info
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //标题
  inputTitle: function(e) {
    console.log(e.detail.value);
    let info = this.data.voteInfo;
    info.title = e.detail.value;
    this.setData({
      title: e.detail.value,
      voteInfo: info
    })
  },
  //题目
  inputSubject: function(e) {
    console.log(e.detail.value);
    //更新题目
    this.setData({
      subjectTitle: e.detail.value
    })
  },
  //添加标题
  addTitle: function() {
    if (this.data.title) {
      this.setData({
        isAddTitle: true
      });
      wx.showToast({
        title: '添加成功',
        icon: "none"
      })
    } else {
      wx.showToast({
        title: '请输入标题',
        icon: "none"
      })
    }
  },
  //添加题目
  addSubject: function() {
    let info = this.data.voteInfo;
    let op = this.data.options;
    info.subject.push({
      subjectTitle: this.data.subjectTitle,
      subjectType:this.data.currentType,
      options: op,
    })

    this.setData({
      voteInfo: info,
      isSubmit:true,
      options:[],
      subjectTitle:"",
      subjectType:0
    });
    wx.showToast({
      title: '添加题目选项成功',
      icon:"none"
    })
    console.log(this.data.voteInfo);
  
  },
  //添加单项
  addSingle: function() {
    this.hideModal();
    this.setData({
      currentType: 1,
      isAdd: true
    })
  },
  //添加多想项
  addMore: function() {
    this.hideModal();
    this.setData({
      currentType: 2,
      isAdd: true
    })
  },
  //添加填空
  addBlank: function() {
    this.hideModal();
    this.setData({
      currentType: 3,
      isAdd: true
    })
  },
  //更新
  inputOption: function(e) {
    let id = e.currentTarget.dataset.id; //输入的选项id
    let option = e.detail.value; //输入的value
    let op = this.data.options;
    console.log("id--->" + id);
    for (let i = 0; i < op.length; i++) {
      if (id === op[i]["id"]) {
        op[i]["option"] = option;
        break;
      }
    }
    this.setData({
      options: op
    });
    console.log("修改咯");
    console.log(this.data.voteInfo);
    //更新当前的题
    // let vote = this.data.voteInfo;
    // let subject = vote["subject"];
    // if (subject){
    //   let index = subject.length;
    //   vote["subject"][index-1]["option"] = op;
    // }
   
  },
  //添加选项
  addOption: function() {
    let op = this.data.options;
    op.push({
      id: op.length + 1,
      option: ""
    });
    this.setData({
      options: op
    });
    console.log(" options.length" + op.length);
  },
  //删除选项
  delectOption(e) {
    let op = this.data.options;
    let id = e.currentTarget.dataset.id;

    for (let i = 0; i < op.length; i++) {
      console.log("i-->" + i + "     id-->" + id);
      if (id === op[i]["id"]) {
        op.splice(i, 1);
        break;
      }
    }
    console.log(op);
    this.setData({
      options: op
    });
  },
  //显示对话框
  showModal: function() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  //提交
  submit: function() {
    if (this.data.voteInfo && this.data.voteInfo.subject.length > 0) {
      //提交用户回答
      let that = this;
      request.postData(
        "?type=add_vote",
        JSON.stringify(that.data.voteInfo),
        (data) => {
          console.log(data);
          if (data.code == 200) {
            wx.switchTab({
              url: '../home/home'
            })
          } else {
            wx.showToast({
              title: data.msg,
            })
          }

        },
        (err) => {

        }
      );
    } else {
      wx.showToast({
        title: '请添加题目',
        icon: "none"
      })
    }
  }
})